package badugaclient;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GuiAuctions extends JPanel 
{
	private JLabel cash = new JLabel("0");
	private JLabel auction = new JLabel("0");
	private JLabel infoLabel = new JLabel();
	private JLabel blindLabel = new JLabel();
	private JLabel maxBet = new JLabel("MAX BET:");
	private JButton check = new JButton("Check");
	private JButton bet = new JButton("Bet");
	private JButton raise = new JButton("Raise");
	private JButton call = new JButton("Call");
	private JButton fold = new JButton("Fold");
	private JButton allIn = new JButton("All-in");
	private JTextField auctionText = new JTextField();
	private Gui gui;
	private PlayerInfo pInfo;
	
	GuiAuctions(Gui gui, PlayerInfo pInfo)
	{
		super();
		this.gui = gui;
		this.pInfo = pInfo;
		setLayout(new GridLayout(8,1));
		add(cash);
		add(auction);
		add(infoLabel);
		add(blindLabel);
		
		add(maxBet);
		add(bet);
		add(auctionText);
		auctionText.setEnabled(false);
		bet.setEnabled(false);
		bet.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0)
			{
				try
				{
					if(Integer.parseInt(auctionText.getText()) < (pInfo.getCurrentTopBet() + 1) || Integer.parseInt(auctionText.getText()) > (pInfo.getMyTokens()))
					{
						auctionText.setText("Wrong value");
					}
					else
					{
						pInfo.setContent("Bet");
						if(pInfo.getSmallID().equals(pInfo.getMyID()))
						{
							pInfo.setMyBet(Integer.toString(Integer.parseInt(auctionText.getText()) - Integer.parseInt(pInfo.getSmallBlind())));
							System.out.println(pInfo.getMyBet());
						}
						else if(pInfo.getBigID().equals(pInfo.getMyID()))
						{
							pInfo.setMyBet(Integer.toString(Integer.parseInt(auctionText.getText()) - Integer.parseInt(pInfo.getBigBlind())));
							System.out.println(pInfo.getMyBet());
						}
						else
						{
							pInfo.setMyBet(auctionText.getText());
							System.out.println(pInfo.getMyBet());
						}
						gui.setChanged();
						gui.notifyObservers();
					}
				}
				catch(NumberFormatException ex)
				{
					
				}
					
				
				
			}
		});
		
		add(raise);
		raise.setEnabled(false);
		raise.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0)
			{
				try
				{
					if(Integer.parseInt(auctionText.getText()) < (pInfo.getCurrentTopBet() + 1) || Integer.parseInt(auctionText.getText()) > (pInfo.getMyTokens()))
					{
						auctionText.setText("Wrong value");
					}
					else
					{
						pInfo.setContent("Raise");
						if(pInfo.getSmallID().equals(pInfo.getMyID()))
						{
							pInfo.setMyRaise(Integer.toString(Integer.parseInt(auctionText.getText()) - Integer.parseInt(pInfo.getSmallBlind())));
						}
						else if(pInfo.getBigID().equals(pInfo.getMyID()))
						{
							pInfo.setMyRaise(Integer.toString(Integer.parseInt(auctionText.getText()) - Integer.parseInt(pInfo.getBigBlind())));
						}
						else
						{
							pInfo.setMyRaise(auctionText.getText());
						}
						gui.setChanged();
						gui.notifyObservers();
					}
				}
				catch(NumberFormatException ex)
				{
					
				}
					
				
				
			}
		});
		add(call);
		call.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0)
			{
				pInfo.setContent("Call");
				gui.setChanged();
				gui.notifyObservers();
			}
		});
		call.setEnabled(false);
		add(fold);
		fold.setEnabled(false);
		fold.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0)
			{
				pInfo.setContent("Fold");
				gui.setChanged();
				gui.notifyObservers();
			}
		});
		add(allIn);
		allIn.setEnabled(false);
		allIn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0)
			{
				pInfo.setContent("All-in");
				gui.setChanged();
				gui.notifyObservers();
			}
		});
		
		add(check);
		check.setEnabled(false);
		check.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0)
			{
				pInfo.setContent("Check");
				gui.setChanged();
				gui.notifyObservers();
			}
		});
	}
	
	public void setCachText(String Text)
	{
		this.cash.setText(Text);
	}
	
	public void setMyBet(String Text)
	{
		this.auction.setText(Text);
	}
	
	public void setInfoLabelText(String Text)
	{
		this.infoLabel.setText(Text);
	}
	
	public void setBlindLabelText(String Text)
	{
		this.blindLabel.setText(Text);
	}
	
	public void setAllInEnable()
	{
		this.allIn.setEnabled(true);
	}
	
	public void setCheckEnable()
	{
		this.check.setEnabled(true);
	}
	
	public void setBetEnable()
	{
		this.bet.setEnabled(true);
		this.auctionText.setEnabled(true);
	}
	
	public void setRaiseEnable()
	{
		this.raise.setEnabled(true);
		this.auctionText.setEnabled(true);
	}
	
	public void setCallEnable()
	{
		this.call.setEnabled(true);
	}
	
	public void setFoldEnable()
	{
		this.fold.setEnabled(true);
	}
	
	public void setFoldDisable()
	{
		this.fold.setEnabled(false);
	}
	
	public void setCheckDisable()
	{
		this.check.setEnabled(false);
	}
	
	public void setAllInDisable()
	{
		this.allIn.setEnabled(false);
	}
	
	public void setCallDisable()
	{
		this.call.setEnabled(false);
	}
	
	public void setBetDisable()
	{
		this.bet.setEnabled(false);
	}
	
	public void setRaiseDisable()
	{
		this.raise.setEnabled(false);
	}
	
	public void setMaxBet(String Text)
	{
		this.maxBet.setText(Text);
	}
}


